package com.win.controller.flume;
import com.jcraft.jsch.*;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;


@Service
public class SftpService {

    private static final String HOST = "192.168.30.130";
    private static final String USER = "root";
    private static final String PASSWORD = "wx041800";
    private static final int PORT = 22;

    public void uploadFile(InputStream inputStream, String remoteFilePath) throws JSchException, SftpException {
        JSch jsch = new JSch();
        Session session = jsch.getSession(USER, HOST, PORT);
        session.setPassword(PASSWORD);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        try (AutoCloseableChannelSftp sftp = new AutoCloseableChannelSftp(session)) {
            sftp.put(inputStream, remoteFilePath);


        } // 在这里，AutoCloseableChannelSftp的close()方法将被自动调用
        catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            session.disconnect();
        }
    }
}